#!/bin/sh
#
# Create change list and fetch files, updated between two repository revisions.

#PREVIOUS_RELEASE="$1"
#CURRENT_RELEASE="$2"
PREVIOUS_RELEASE="1.3.0"
CURRENT_RELEASE="1.3.1"
OUTPUT_DIR=`pwd`
FILES_LIST="$OUTPUT_DIR/$CURRENT_RELEASE/files.txt"
FILES_DIR="$OUTPUT_DIR/$CURRENT_RELEASE/files"

self=`basename $0`
tmpdir=`mktemp -d -t $self`

REPOSITORY_DIR="$tmpdir/platonica"

tmpfile=`mktemp -t $self`

[ -f "$FILES_LIST" ] && rm -f "$FILES_LIST"
[ -d "$FILES_DIR" ] && rm -fr "$FILES_DIR"
mkdir -p "$FILES_DIR"

(
echo Pull repository to "$REPOSITORY_DIR"
hg clone http://platonica.ru/hg "$REPOSITORY_DIR" #> /dev/null
cd "$REPOSITORY_DIR"
echo Update repository to version "$PREVIOUS_RELEASE"
hg update -r "$PREVIOUS_RELEASE" #> /dev/null
# put file change log to temp file
hg -v update -r "$CURRENT_RELEASE" > "$tmpfile"
)

# remove first and last (status) lines from mercurial log
cat $tmpfile \
    | sed '1d;$d' \
    | grep -E '^[[:alpha:]]+ (admin|bin|data|etc|eventsfetcher|install|lib|skins|var|index\.php|indexing\.php|install\.txt|LICENSE|init_fu\.php|racron\.php|racron\.sh)' \
    > "$FILES_LIST"

# copy each updated file to FILES_DIR
cat "$FILES_LIST" \
    | grep -E '^getting' \
    | cut -c9- \
    | while read line; do
	src="$REPOSITORY_DIR/$line"
	dst="$FILES_DIR/$line"
	dst_dir=`dirname "$dst"`
	[ ! -d "$dst_dir" ] && mkdir -p "$dst_dir"
	cp -v "$src" "$dst"
done

rm -f $tmpfile
rm -rf $REPOSITORY_DIR

